# Make LaTeX chunk for Table 3

# Load Models and Store AUCs
dvs <- c("any", "high", "spike")
models <- c('full', "lagdv", 
            "snpkagg",
            "snpk", "onlyviolence")

auc_array <- array(NA,
                   dim = c(length(dvs),
                           length(models)),
                   dimnames = list(dvs,
                                   models))
algo <- "ebma"
country <- 'indo'
for (x in models) {
  for (dv in dvs) {
    if (x=='lagdv') {
      filename <- paste(modeldir,"/",
                        country,
                        "_lagdv_",
                        dv,
                        ".RData",
                        sep = "")
      algo <- 'fe'
    } else {
      filename <- paste(modeldir,"/",
                        country,
                        "_ebma_",
                        dv,
                        "_", x, ".RData",
                        sep = "")  
      algo <- 'ebma'
    }
    load(filename)
    aucs_by_year = c()
    for (i in 1:length(get(paste(algo,".results",sep="")))) {
      predictions <- as.vector(get(paste(algo,".results",sep=""))[[i]]$fit.oos)
      realizations <- as.vector(get(paste(algo,".results",sep=""))[[i]]$actual.oos)
      aucs_by_year <- c(aucs_by_year,
                        roc(response = realizations,
                            predictor = predictions)[['auc']])
    }
    auc_array[dv, x] <- mean(aucs_by_year)
  }
}


# Build Table with Labels
outcome.labels <- c(any="Any violent event",
                    high="$\\ge$ 5 violent events",
                    spike="$\\ge$ 1 s.d. increase in events")
model.labels <- c(full='Full\\\\ Predictors',
                  lagdv='Only \\\\ Lagged \\\\Indicator',
                  snpkagg='Add \\\\ Intensity',
                  snpk='Disaggregate \\\\ Violence',
                  onlyviolence='All\\\\ Lagged \\\\ Violence \\\\ Data')

table <- auc_array
dimnames(table)[[1]] <- outcome.labels[dimnames(table)[[1]]]
dimnames(table)[[2]] <- model.labels[dimnames(table)[[2]]]

# Print to Latex
printA5(table = table,
        filepath = "tables",
        file = "table_A5",
        head.width = '1.8')

